﻿using DDS.IoT.Com;
using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace DDS.IoT.Modbus
{
    public class ModbusTcpCom : ComBase
    {
        public override string Type => "modbusTcpCom";

        public override string Name => "ModbusTcpCom";
        private object _locker = new object();

        public override bool Init(int port, int baudRate = 9600, string extendData = null)
        {
            this.Port = port;
            ModbusTcpService.Init(extendData, port);
            Console.WriteLine("初始化ModbusTcpCom驱动程序成功！");
            return true;
        }
        public override event PushData OnPushData;

        public override bool Open()
        {
            var b = false;
            try
            {
                ModbusTcpService.Open();
                b = true;
                IsOpen = true;
            }
            catch (Exception ex)
            {

                string msg = string.Format("ModbusTcpCom串口打开失败：{0} ", ex.Message);
                Console.WriteLine(msg);
            }
            return b;
        }

        public override bool Close()
        {
            ModbusTcpService.Close();
            IsOpen = false;
            OnPushData = null;
            return true;
        }

        public override string Command(string name, string data)
        {
            var outData = string.Empty;
            var dataObj = Newtonsoft.Json.JsonConvert.DeserializeObject<dynamic>(data);
            switch (name)
            {
                case "ReadCoils":
                    //01
                    var readData = ModbusTcpService.ReadCoils(byte.Parse(dataObj.slaveAddress.ToString()), ushort.Parse(dataObj.startAddress.ToString()), ushort.Parse(dataObj.numberOfPoints.ToString()));
                    outData = ModbusHelper.ToString(readData);
                    break;
                case "ReadInputs":
                    //02
                    readData = ModbusTcpService.ReadInputs(byte.Parse(dataObj.slaveAddress.ToString()), ushort.Parse(dataObj.startAddress.ToString()), ushort.Parse(dataObj.numberOfPoints.ToString()));
                    outData = ModbusHelper.ToString(readData);
                    break;
                case "ReadHoldingRegisters":
                    //03
                    readData = ModbusTcpService.ReadHoldingRegisters(byte.Parse(dataObj.slaveAddress.ToString()), ushort.Parse(dataObj.startAddress.ToString()), ushort.Parse(dataObj.numberOfPoints.ToString()));
                    outData = ModbusHelper.ToString(readData);
                    break;
                case "ReadInputRegisters":
                    //04
                    readData=ModbusTcpService.ReadInputRegisters(byte.Parse(dataObj.slaveAddress.ToString()), ushort.Parse(dataObj.startAddress.ToString()), ushort.Parse(dataObj.numberOfPoints.ToString()));
                    outData = ModbusHelper.ToString(readData);
                    break;
                case "WriteSingleCoil":
                    //05
                    ModbusTcpService.WriteSingleCoil(byte.Parse(dataObj.slaveAddress.ToString()), ushort.Parse(dataObj.startAddress.ToString()), ModbusHelper.BoolParse(dataObj.value.ToString()));
                    break;
                case "WriteSingleRegister":
                    //06
                    ModbusTcpService.WriteSingleRegister(byte.Parse(dataObj.slaveAddress.ToString()), ushort.Parse(dataObj.startAddress.ToString()), ushort.Parse(dataObj.value.ToString()));
                    break;
                case "WriteMultipleCoils":
                    //0F 写一组线圈
                    var values = dataObj.value.ToString().Split(' ');
                    var datas =new bool[values.Length];
                    for(var i=0;i< values.Length;i++)
                    {
                        datas[i] = ModbusHelper.BoolParse(values[i]);
                    }
                    ModbusTcpService.WriteMultipleCoils(byte.Parse(dataObj.slaveAddress.ToString()), ushort.Parse(dataObj.startAddress.ToString()), datas);
                    break;
                case "WriteMultipleRegisters":
                    // 10 写一组保持寄存器
                    values = dataObj.value.ToString().Split(' ');
                    var udatas = new ushort[values.Length];
                    for (var i = 0; i < values.Length; i++)
                    {
                        udatas[i] = ushort.Parse(values[i]);
                    }
                    ModbusTcpService.WriteMultipleRegisters(byte.Parse(dataObj.slaveAddress.ToString()), ushort.Parse(dataObj.startAddress.ToString()), udatas);
                    break;
                    
            }
            return outData;
        }
    }
}
